package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Attendance;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.AttendanceMapper;
import cn.wolfcode.crm.query.AttendanceQueryObject;
import cn.wolfcode.crm.service.IAttendanceService;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/6/12.
 */

@Service
public class AttendanceServiceImpl implements IAttendanceService {

    @Autowired
    AttendanceMapper attendanceMapper;


    @Override
    public PageResult query(AttendanceQueryObject qo) {
        int count = attendanceMapper.query4Count(qo);
        if (count > 0) {
            return new PageResult(count, attendanceMapper.query4List(qo));
        }
        return new PageResult(count, Collections.EMPTY_LIST);
    }
    @Override
    public void saveUp() {
        Date date = new Date();
        SimpleDateFormat attDate = new SimpleDateFormat("yyyy-MM-dd");
        String hh = new SimpleDateFormat("HH").format(date);
        String mm = new SimpleDateFormat("mm").format(date);
        Attendance attendance = new Attendance();
        Employee employee = UserContext.getCurrentUser();
        List<Attendance> attendances = attendanceMapper.selectByEmpId(employee.getId());
//        Collections.reverse(attendances);//集合倒序
        if (attendances.size()!=0){
            if (attendances.get(attendances.size()-1).getEmployee().getUsername().equals(employee.getUsername()) && !attDate.format(date).equals(attDate.format(attendances.get(attendances.size() - 1).getDate()))){
                attendance.setDate(date);
                attendance.setEmployee(employee);
                attendance.setDept(employee.getDept());
                attendance.setTimeIn(date);
                //判断是否迟到
                if(Integer.parseInt(hh) <= 8 && Integer.parseInt(hh) <=30 ){
                    attendance.setState(true);
                }else{
                    attendance.setState(false);
                }
                attendanceMapper.insert(attendance);
            }else {
                throw new LogicException("亲,你已签到,请明天准时在过来签到");
            }
        }else {
            attendance.setDate(date);
            attendance.setEmployee(employee);
            attendance.setDept(employee.getDept());
            attendance.setTimeIn(date);
            //判断是否迟到
            if(Integer.parseInt(hh) <= 8 && Integer.parseInt(hh) <=30 ){
                attendance.setState(true);
            }else{
                attendance.setState(false);
            }
            attendanceMapper.insert(attendance);
        }
    }

    @Override
    public void saveNext() {
        Date date = new Date();
        SimpleDateFormat attDate = new SimpleDateFormat("yyyy-MM-dd");
        String hh = new SimpleDateFormat("HH").format(date);
        String mm = new SimpleDateFormat("mm").format(date);
        String dd = new SimpleDateFormat("dd").format(date);
        Employee employee = UserContext.getCurrentUser();
        List<Attendance> attendances = attendanceMapper.selectByEmpId(employee.getId());
        if (attendances.size()!=0){
            Attendance attendance = attendances.get(attendances.size() - 1);
            if (attendance.getEmployee().getUsername().equals(employee.getUsername()) && attDate.format(date).equals(attDate.format(attendance.getDate()))) {
                if (attendance.getTimeOot() == null){
                    String add = new SimpleDateFormat("dd").format(attendance.getDate());
                    if (Integer.parseInt(dd) == Integer.parseInt(add)) {
                        attendance.setTimeOot(date);
                        //判断是否早退
                        if (Integer.parseInt(hh) >= 17 && Integer.parseInt(mm) >= 30) {
                            attendance.setState(true);
                        } else {
                            attendance.setState(false);
                        }
                        attendanceMapper.updateByPrimaryKey(attendance);
                    }
                }else {
                    throw new LogicException("亲,你已经签退了!!!");
                }
            }else{
                throw new LogicException("亲,你还没签到呢!!!");
            }
        }else{
            throw new LogicException("亲,你还没签到呢!!!");
        }

    }
}
